from hep.aida import IAnalysisFactory

from java.awt import Color
from java.awt import Font
from java.lang import System
from java.util import Locale
from java.io import File


from jhplot.jadraw import JaKey
from jhplot  import H1D
from jhplot  import HPlotJa


#from fr.in2p3.ipnl.aidautilities import *

from jscinttools.utilities.aidautilities import AidaFileReader
from jscinttools.utilities.aidautilities import GrabIObject
from jscinttools.utilities.jhplotutilities import HPlotJaUtil

Locale.setDefault(Locale.ENGLISH)
System.getProperties().setProperty("hep.aida.BatchAnalysisFactory","hep.aida.ref.BatchAnalysisFactory")
af = IAnalysisFactory.create();

showFigures = False
writeFigures = not showFigures

""" Define colors and fonts"""
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 25)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)

""" Define the Path and files names"""
aidaPath = "/home/marc-antoine/Documents/ZnWO4/DAQ_Trig/"
aidaFileS = "Na22_3200mK_001_PhotonReanalysis_NoTuples"
aidaFileD = "Na22_3200mK_002_Ch1Ch2TrigCube_PhotonReanalysis_NoTuples"

avEvtOutFile = "ZnWO4_AvEvt_3200mK_DAQ_Comp"
avEvtZoomOutFile = "ZnWO4_AvEvtZoom_3200mK_DAQ_Comp"
specOutFile = "ZnWO4_Spec_3200mK_DAQ_Comp"
outPath = "/home/marc-antoine/Dropbox/ZnWO4/"

avEvtPathS = "Reanalysis/Average pulse shapes/Variable binning/Weighted Average Pulse from First Photon on Ch1, Ch0, All Cuts"
avEvtPathD = "Reanalysis/Average pulse shapes/Variable binning/Weighted Average Pulse from Reference Time, Ch0, All Cuts"

avEvtPathZoomS = "Reanalysis/Average pulse shapes/Zoom/Weighted Average Pulse from First Photon on Ch1, Ch0, All Cuts"
avEvtPathZoomD = "Reanalysis/Average pulse shapes/Zoom/Weighted Average Pulse from Reference Time, Ch0, All Cuts"

specPathS = "Reanalysis/Number of Single Photons, Ch0, All Events"
specPathD = "Reanalysis/Number of Single Photons, Ch0, All Events"


treeS = AidaFileReader(aidaPath, aidaFileS).tree
treeD = AidaFileReader(aidaPath, aidaFileD).tree

avEvtHistoS = GrabIObject(treeS, avEvtPathS).histo1D
avEvtHistoS.scale(1.0/avEvtHistoS.maxBinHeight())

avEvtHistoD = GrabIObject(treeD, avEvtPathD).histo1D
avEvtHistoD.scale(1.0/avEvtHistoD.maxBinHeight())

avEvtHistoZoomS = GrabIObject(treeS, avEvtPathZoomS).histo1D
avEvtHistoZoomS.scale(1.0/avEvtHistoZoomS.maxBinHeight())

avEvtHistoZoomD = GrabIObject(treeD, avEvtPathZoomD).histo1D
avEvtHistoZoomD.scale(1.0/avEvtHistoZoomD.maxBinHeight())

specHistoS = GrabIObject(treeS, specPathS).histo1D
specHistoS.scale(1.0/specHistoS.maxBinHeight())

specHistoD = GrabIObject(treeD, specPathD).histo1D
specHistoD.scale(1.0/specHistoD.maxBinHeight())


"""
###############################################################
# Plotting Part
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects"""

avEvtS = H1D(avEvtHistoS, title="Standard", style="h", color=colors[0], penWidth=2, errAll=False)
avEvtD = H1D(avEvtHistoD, title="Double", style="h", color=colors[1], penWidth=2, errAll=False)
avEvtZoomS = H1D(avEvtHistoZoomS, title="Standard", style="h", color=colors[0], penWidth=2, errAll=False)
avEvtZoomD = H1D(avEvtHistoZoomD, title="Double", style="h", color=colors[1], penWidth=2, errAll=False)
specS = H1D(specHistoS, title="Standard", style="h", color=colors[0], penWidth=2, errAll=False)
specD = H1D(specHistoD, title="Double", style="h", color=colors[1], penWidth=2, errAll=False)

avEvtKeyS = JaKey(20, avEvtS.title, 0.6, 0.2, "NDC", color=avEvtS.color, keyColor=avEvtS.color, font=legendFont)
avEvtKeyD = JaKey(20, avEvtD.title, 0.6, 0.25, "NDC", color=avEvtD.color, keyColor=avEvtD.color, font=legendFont)
avEvtZoomKeyS = JaKey(20, avEvtZoomS.title, 0.6, 0.2, "NDC", color=avEvtZoomS.color, keyColor=avEvtZoomS.color, font=legendFont)
avEvtZoomKeyD = JaKey(20, avEvtZoomD.title, 0.6, 0.25, "NDC", color=avEvtZoomD.color, keyColor=avEvtZoomD.color, font=legendFont)
specKeyS = JaKey(20, specS.title, 0.6, 0.2, "NDC", color=specS.color, keyColor=specS.color, font=legendFont)
specKeyD = JaKey(20, specD.title, 0.6, 0.25, "NDC", color=specD.color, keyColor=specD.color, font=legendFont)




""" Create the Plotter"""
avPlotter = HPlotJa("Average Pulse ZnWO4 22Na 3.2 K", 600, 600, 1, 1, True)
avPlotter.setGTitle("Average Pulse ZnWO4 22Na 3.2 K", titleFont)
avPlotter.antiAlias = False
if showFigures:
	avPlotter.visible(True)
else:
	avPlotter.visible(False)	
avPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
avPlotter.setRange(0, 1800, 0.00001, 1)
avPlotter.setLogScale(0, False)
avPlotter.setLogScale(1, True)
avPlotter.ticFont = ticFont
avPlotter.showStatBox = False
avPlotter.showKey(False)
avPlotter.nameX = "Time (&micro;s)"
avPlotter.nameY = "Photons / Time Unit"
""" Plots all the JHep objects"""
avPlotter.draw(avEvtS)
avPlotter.draw(avEvtD)
avPlotter.draw(avEvtKeyS)
avPlotter.draw(avEvtKeyD)

avAxes = avPlotter.getPad()
avAxes.setLabelSpace(0, 0.0)
avAxes.setLabelSpace(1, -0.025)
HPlotJaUtil.setTickLabelOneToPower(1, avAxes)
HPlotJaUtil.setTickLabelZeroPositive(avAxes)

""" Modifies the JaAxes and the axes labels"""
avLabX = avPlotter.getLabelX()[0][0]
avLabX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, avLabX, avPlotter, 0.025)
avLabY = avPlotter.getLabelY()[0][0]
avLabY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, avLabY, avPlotter, -0.01)
 
avPlotter.update()

##############################################################################

avZoomPlotter = HPlotJa("Average Pulse Zoom ZnWO4 22Na 3.2 K", 600, 600, 1, 1, True)
avZoomPlotter.setGTitle("Average Pulse Zoom ZnWO4 22Na 3.2 K", titleFont)
avZoomPlotter.antiAlias = False
if showFigures:
	avZoomPlotter.visible(True)
else:
	avZoomPlotter.visible(False)	
avZoomPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
avZoomPlotter.setRange(0, 2, 0.01, 1)
avZoomPlotter.setLogScale(0, False)
avZoomPlotter.setLogScale(1, True)
avZoomPlotter.ticFont = ticFont
avZoomPlotter.showStatBox = False
avZoomPlotter.showKey(False)
avZoomPlotter.nameX = "Time (&micro;s)"
avZoomPlotter.nameY = "Photons / Time Unit"
""" Plots all the JHep objects"""
avZoomPlotter.draw(avEvtZoomS)
avZoomPlotter.draw(avEvtZoomD)
avZoomPlotter.draw(avEvtZoomKeyS)
avZoomPlotter.draw(avEvtZoomKeyD)

avZoomAxes = avZoomPlotter.getPad()
avZoomAxes.setLabelSpace(0, 0.0)
avZoomAxes.setLabelSpace(1, -0.025)
HPlotJaUtil.setTickLabelOneToPower(1, avZoomAxes)
HPlotJaUtil.setTickLabelZeroPositive(avZoomAxes)

""" Modifies the JaAxes and the axes labels"""
avZoomLabX = avZoomPlotter.getLabelX()[0][0]
avZoomLabX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, avZoomLabX, avZoomPlotter, 0.025)
avZoomLabY = avZoomPlotter.getLabelY()[0][0]
avZoomLabY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, avZoomLabY, avZoomPlotter, -0.01)
 
avZoomPlotter.update()


##############################################################################

specPlotter = HPlotJa("Spectra ZnWO4 22Na 3.2 K", 600, 600, 1, 1, True)
specPlotter.setGTitle("Spectra ZnWO4 22Na 3.2 K", titleFont)
specPlotter.antiAlias = False
if showFigures:
	specPlotter.visible(True)
else:
	specPlotter.visible(False)	
specPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
specPlotter.setRange(0, 150, 0, 1.1)
specPlotter.setLogScale(0, False)
specPlotter.setLogScale(1, False)
specPlotter.ticFont = ticFont
specPlotter.showStatBox = False
specPlotter.showKey(False)
specPlotter.nameX = "Number of Photons"
specPlotter.nameY = "Counts (a.u.)"
""" Plots all the JHep objects"""
specPlotter.draw(specS)
specPlotter.draw(specD)
specPlotter.draw(specKeyS)
specPlotter.draw(specKeyD)

specAxes = specPlotter.getPad()
specAxes.setLabelSpace(0, 0.0)
specAxes.setLabelSpace(1, 0.0)
#HPlotJaUtil.setTickLabelOneToPower(1, specAxes)
HPlotJaUtil.setTickLabelZeroPositive(specAxes)

""" Modifies the JaAxes and the axes labels"""
specLabX = specPlotter.getLabelX()[0][0]
specLabX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, specLabX, specPlotter, 0.025)
specLabY = specPlotter.getLabelY()[0][0]
specLabY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, specLabY, specPlotter, -0.01)
 
specPlotter.update()


""" Saves or not the plot"""
if writeFigures:
	for i in range(len(figType)):
		avPlotter.export(outPath + File.separator + avEvtOutFile + figType[i])
		avZoomPlotter.export(outPath + File.separator + avEvtZoomOutFile + figType[i])
		specPlotter.export(outPath + File.separator + specOutFile + figType[i])
		print("Writting File : " + outPath + File.separator + avEvtOutFile + figType[i])
		print("Writting File : " + outPath + File.separator + avEvtZoomOutFile + figType[i])
		print("Writting File : " + outPath + File.separator + specOutFile + figType[i])
		
print("That's all folks !")

